package cn.kasa.system.controller;

import cn.kasa.basic.utils.AjaxResult;
import cn.kasa.basic.utils.PageList;
import cn.kasa.system.domain.Dictionary;
import cn.kasa.system.domain.DictionaryDetail;
import cn.kasa.system.query.DictionaryDetailQuery;
import cn.kasa.system.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping
    public List<DictionaryDetail> loadAll(){
       return dictionaryDetailService.loadAll();
    }

    @GetMapping("/{id}")
    public DictionaryDetail loadById(@PathVariable("id") Long id){
        return dictionaryDetailService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    @PatchMapping
    public AjaxResult batchDictionaryDetail(@RequestBody  Long[] ids){
        try {
            dictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping
    public PageList<DictionaryDetail> queryData(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        Long id = dictionaryDetailQuery.getId();
        PageList<DictionaryDetail> dictionaryDetails = dictionaryDetailService.queryData(dictionaryDetailQuery);
        return dictionaryDetails;
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody DictionaryDetail dictionaryDetail){
        try{
            if(dictionaryDetail.getId() == null){
                dictionaryDetailService.save(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
